package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.SellBean;
import com.project.bean.SellCarBean;
import com.project.mapper.IProductMapper;
import com.project.mapper.ISellMapper;
import com.project.service.ISellService;
import org.apache.commons.logging.impl.SimpleLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Map;

@Service
public class SellServiceImpl implements ISellService {
    @Autowired
    private ISellMapper mapper;
    @Autowired
    private IProductMapper productMapper;
    @Override
    public void add(SellBean sell, Map<Integer, SellCarBean> sellMap) {
        mapper.addSell(sell);
        mapper.addSellInfo(sell.getId(), sellMap.values());

        for(SellCarBean s :sellMap.values()){
            productMapper.updateStoreNum(s.getProduct().getId(), -s.getNum());
        }

    }

    @Override
    public PageInfo<SellBean> findByItem(int pageNO, String name, LocalDate startDate, LocalDate endDate) {
        PageHelper.startPage(pageNO, 3);
        PageInfo pageInfo = PageInfo.of(mapper.findByItem(name,startDate,endDate));
        return pageInfo;
    }

    @Override
    public SellBean findById(int sellId) {
        return mapper.findById(sellId);
    }
}
